package org.stwy.job;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.stwy.lease.contract.ContractEntity;
import org.stwy.lease.contract.ContractService;

import com.rap.common.exception.ServiceException;
import com.rap.common.utils.DateUtil;
import com.rap.frame.job.RapJob;
import com.rap.frame.job.schedule.JobSchedule;

/**
 * 每天晚上11点59分查找所有当天到期的合同，将其状态置为终止。
 * @author wzw
 *
 */
@Service("contractEndJob")
public class ContractEndJob implements RapJob, Serializable {
	private static final long serialVersionUID = -4024068316923801453L;
	private static Logger logger = LoggerFactory.getLogger(ContractEndJob.class);
	@Autowired
	private ContractService contractService;
	
	@Override
	public void execute(JobSchedule schedule)
			throws JobExecutionException {
		logger.info("=============开始执行： ContractEndJob==========");
		try{
			Date date = DateUtil.getCurDate();
			List<ContractEntity> list = contractService.findNotEndContractsByEndDate(date);
			for(ContractEntity contract : list){
				contract.setContractChangeType(ContractEntity.CONTRACT_CHANGE_TYPE_END);
				contract.setContractState(ContractEntity.CONTRACT_STATE_INVALID);
				contract.setReason("ContractEndJob:系统自动终止");
			}
			contractService.save(list);
		}catch(ServiceException e){
			e.printStackTrace();
			logger.error("=============ServiceException:"+e.getMessage()+" ==========");
		}catch(Exception e){
			e.printStackTrace();
			logger.error("=============Exception:"+e.getMessage()+" ==========");
		}
		logger.info("=============ContractEndJob 执行完毕  ==========");
	}

}